package com.foxit.adapp;

import java.io.*;
import java.net.*;
import java.lang.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Arrays;
import java.util.Random;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class FoxitReader1 extends HttpServlet {
    private String GETADURL="";

    public void init(ServletConfig config) throws ServletException{
       super.init(config);
       ServletContext context = getServletContext();
       GETADURL =  context.getInitParameter("GETADURL");
       if(GETADURL==null || GETADURL.length()==0) {
          GETADURL = "http://localhost:8080/foxit-ad-setting-web/Getad1";
       }
    }


    public void doGet(HttpServletRequest request, HttpServletResponse response)
                    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        String udid = request.getParameter("udid");
        String lang = request.getParameter("lang");
        String nation = request.getParameter("nation");
        String adflag = request.getParameter("adflag");
        String member = request.getParameter("member");
        String server = request.getParameter("server");
        String trace_info = request.getParameter("trace_info");
        String clickad = request.getParameter("clickad");
        if(udid==null || udid.length()==0)
           udid = "00-50-8D-9F-91-E4";
        if(lang==null || lang.length()==0)
           lang = "cn";
        if(nation==null || nation.length()==0)
           nation = "cn";
        if(adflag==null || adflag.length()==0)
           adflag = "0";
        if(member==null || member.length()==0)
           member = "1";
        if(server==null || server.length()==0)
           server = "reader";
        if(trace_info==null || trace_info.length()==0)
           trace_info = "upload_file_cloud,open_file_cloud";
        if(clickad==null || clickad.length()==0)
           clickad=""; 
        HashMap map=new HashMap();
        map.put("udid",udid);
        map.put("nation",nation);
        map.put("lang",lang);
        map.put("adflag",Integer.valueOf(adflag));
        map.put("member",Integer.valueOf(member));
        map.put("server",server);
        map.put("trace_info",trace_info);
        map.put("clickad",clickad);
        String mjson=JSON.toJSONString(map, SerializerFeature.UseSingleQuotes);
        out.println("<HTML><head><style>table,th,td { border:1px solid black;border-collapse:collapse; } </style></head><body>");
        out.println("<table border='1' style='width:300px'><tr><th colspan=5>FOXIT AD TEST</th></tr>");
        out.println("<tr><th>show_mode</th><th>type</th><th>ad</th><th>img</th><th>rd</th></tr>");
        HttpClient httpClient = new DefaultHttpClient();
        try {
           HttpPost req = new HttpPost(GETADURL);
           StringEntity params =new StringEntity(mjson);
           req.addHeader("content-type", "application/x-www-form-urlencoded");
           req.setEntity(params);
           HttpResponse resp = httpClient.execute(req);
           if (  resp.getStatusLine().getStatusCode() == 200 ) {
              BufferedReader rd = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
              StringBuffer result = new StringBuffer();
              String line = "";
              while ((line = rd.readLine()) != null) {
                 result.append(line);
              }
              JSONObject json = JSON.parseObject(result.toString());
              int status= json.getInteger("status");
              if(status == 0) {
                 JSONArray info=json.getJSONArray("info");
                 if(!info.isEmpty()) {
                    for(Object o:info) {
                       JSONObject j=(JSONObject)o;
                       String show_mode=j.getString("showMode");
                       out.println("<tr><td>"+show_mode+"</td>");
                       int type=j.getInteger("type");
                       switch(type) {
                          case 1:
                             out.println("<td>動作廣告</td>");
                             break; 
                          case 2:
                             out.println("<td>分組廣告</td>");
                             break; 
                          case 3:
                             out.println("<td>相關廣告</td>");
                             break; 
                          case 4:
                             out.println("<td>輪播廣告</td>");
                             break;
                          default:
                             out.println("<td>其他廣告</td>");
                             break;
                       }
                       JSONArray ad=j.getJSONArray("jsonAD");
                       for(Object p:ad) {
                          JSONObject k=(JSONObject)p;
                          String img=k.getString("img");
                          String r=k.getString("redirect");
                          out.println("<td><a href='"+r+"'><img src='"+img+"'></a></td><td>"+img+"</td><td>"+r+"</td>");
                       } 
                       out.println("</tr>");
                    }
                 }
              }
           }
              
        } catch(Exception ex) {
        } finally {
           httpClient.getConnectionManager().shutdown();
        }
        out.println("</BODY>");
        out.println("</HTML>");
    }
} 


